package com.hihusky;

import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Utils {

    public Utils() {
    }

    public static OkHttpClient okHttpClient = new OkHttpClient().newBuilder().connectTimeout(60, TimeUnit.SECONDS)
            .build();

    public static ObjectMapper objectMapper = new ObjectMapper();

    /**
     * calculate delay time(unit: ms)
     * @param submittingTime the daily time of submitting.
     * @return delay time
     */
    static public Long delayTime(String submittingTime) {
        // get now date
        Date now=new Date();
        Calendar calendar = Calendar.getInstance();

        // get now calendar
        calendar.setTime(now);
        Date today = calendar.getTime();

        // add 1 day from now
        // tomorrow calendar
        calendar.add(Calendar.DATE,1);
        Date tomorrow = calendar.getTime();

        SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String todayDate = dateFormatter.format(today);
        String todayTime = todayDate + " " + submittingTime;
        String tomorrowDate = dateFormatter.format(tomorrow);
        String tomorrowTime = tomorrowDate + " " + submittingTime;
        try {
            Date todaySubmittingTime = timeFormatter.parse(todayTime);
            Date tomorrowSubmittingTime = timeFormatter.parse(tomorrowTime);
            return todaySubmittingTime.getTime() > now.getTime()? todaySubmittingTime.getTime() - now.getTime():tomorrowSubmittingTime.getTime() - now.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

}
